package com.tiemei.demo.unittest.mockito;

import java.awt.List;

import org.junit.Test;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**
 * 连续调用，不同返回值
 * 
 * @author tiemei.jdk
 * @date 2012-10-15
 */
public class StubConsecutiveCalls {

	@Test
	public void test() {
		// 1. mock
		InnerClass mock = mock(InnerClass.class);
		when(mock.someMethod("some arg")).thenThrow(new RuntimeException()).thenReturn("foo");

		// 2. run
		boolean flag = false; // 标示有没有跑出RuntimeException
		try {
			mock.someMethod("some arg"); // RuntimeException
		}
		catch (RuntimeException e) {
			flag = true;
		}
		finally {
			System.out.println(mock.someMethod("some arg")); // "foo"
			// //Any consecutive call: prints "foo" as well (last stubbing wins).
			System.out.println(mock.someMethod("some arg")); // "foo"
		}
		
		// 3. verify
		verify(mock, times(3)).someMethod("some arg");
		assertTrue(flag);
	}

	@Test
	public void testShorterVersion() {
		InnerClass mock = mock(InnerClass.class);
		when(mock.someMethod("some arg")).thenReturn("one", "two", "three");
	}

	public static class InnerClass {

		public String someMethod(String arg) {
			return null;
		}
	};
}
